﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRManagerUI.EmpSalary
{
    public partial class SalaryIncrement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                incrementshowDiv.Visible = false;

            }
            ClearInfo();

        }

        private void ClearInfo()
        {
            skillImage.Src = "";
            puncImage.Src = "";
            commImage.Src = "";
            imgTD.Visible = false;
            generalEmpInfo.Visible = false;
        }

        protected void SaveIncrement(object sender, EventArgs e)
        {
            try
            {
                Employee employee = new Employee();
                Code code = new Code();
                code.CodeName = txtEmpCode.Text;
                //  ViewState["code"] = txtEmpCode.Text;
                employee.EmpCode = code;
                IncrementManager manager = new IncrementManager();
                Increment increment = new Increment();
                Salary salary = new Salary();
                increment.Employee = employee;
                increment.Basic = Convert.ToDecimal(txtBasic.Value);
                increment.Medical = Convert.ToDecimal(txtMedical.Value);
                increment.Residence = Convert.ToDecimal(txtResidence.Value);
                increment.Transport = Convert.ToDecimal(txtTransport.Value);
                increment.PF = Convert.ToDecimal(txtProfident.Value);
                increment.Mobile = Convert.ToDecimal(txtMobile.Value);
                increment.Others = Convert.ToDecimal(txtOthers.Value);
                increment.Gross = Convert.ToDecimal(txtGross.Value);
                increment.Date = Convert.ToDateTime(txtDate.Value);
                // increment.IncrementNumber = Convert.ToInt32(txtIncrementNumber.Value);
                increment.TotalIncrement = Convert.ToInt32(txtTotalIncrement.Value);
                string msg = manager.SaveIncrement(increment);

                if (msg.Contains("Successfully"))
                {
                    imgTD.Visible = true;
                    generalEmpInfo.Visible = true;
                    incrementshowDiv.Visible = true;
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Text = "Salary Incremented Successfully for Employee " + "-" + code.CodeName;
                    PopulateIncrement();
                    skillImage.Visible = true;
                    puncImage.Visible = true;
                    commImage.Visible = true;
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = msg;


                }


            }
            catch (Exception ex)
            {

                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = ex.Message;
            }

        }

        public void PopulateIncrement()
        {

            Employee employee = new Employee();
            Code code = new Code();
            code.CodeName = txtEmpCode.Text;
            employee.EmpCode = code;

            Increment increment = new Increment();
            increment.Employee = employee;

            List<Increment> increments = new List<Increment>();
            IncrementManager incrementManager = new IncrementManager();

            increments = incrementManager.GetIncrementInfo(increment);
            salaryIncrementGdv.DataSource = increments;
            salaryIncrementGdv.DataBind();

            var count = increments.Count;

            if (count > 0)
            {
                countIncrement.Text = lblempName.Text + "'s " + "Salary has been incremnted " + count + " times";
                countIncrement.ForeColor = Color.Green;

            }
            else
            {
                countIncrement.Text = lblempName.Text + "'s " + "Salary has not incremnted before";
                countIncrement.ForeColor = Color.Red;
            }

            imgTD.Visible = true;
            generalEmpInfo.Visible = true;
            incrementshowDiv.Visible = true;


        }

        protected void salaryIncrementGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            salaryIncrementGdv.PageIndex = e.NewPageIndex;
            PopulateIncrement();
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {

           PopulateEmployeeInfo();
           PopulateIncrement();
           
        }

        private void PopulateEmployeeInfo()
        {
            try
            {
                //Employee General Info
                msgLabel.Text = "";
                Code code = new Code();
                code.CodeName = txtEmpCode.Text;
                ViewEmployee viewEmployee = new ViewEmployee();
                EmployeeManager manager = new EmployeeManager();
                viewEmployee = manager.SearchEmployeeByCode(code);
                lblEmpStatus.Text = viewEmployee.Employee.Status;
                lblempName.Text = viewEmployee.Employee.Name;
                lblDesignation.Text = viewEmployee.Designation.Title;
                lblProject.Text = viewEmployee.Project.ProjectName;
                lblJoiningDate.Text = viewEmployee.Employee.JoiningDate.ToShortDateString();
                empImage.ImageUrl = viewEmployee.Photo.FileName;
                
                //Employee Evaluation Info
                EvaluationManager evaluationManager = new EvaluationManager();
                SingleEmployeEvaluationReport report = new SingleEmployeEvaluationReport();
                report = evaluationManager.GetEmployeesEvaluation(code);
                lblEvaluatedBy.Text = report.Pm;
                lblEvaDate.Text = report.Date;
                lblSkill.Text = report.Skill;
                lblCommunication.Text = report.Communication;
                lblPunctuality.Text = report.Punctuality;
                lblComments.Text = '"' + report.Comments + '"';
                imgTD.Visible = true;
                generalEmpInfo.Visible = true;


                switch (report.Skill)
                {
                    case "Low": skillImage.Src = "~/images/LowPerformance.png"; break;
                    case "Avarage": skillImage.Src = "~/images/AverageProgress.png"; break;
                    case "High": skillImage.Src = "~/images/HighProgress.png"; break;
                    default: skillImage.Alt = "Not Updated Yet"; break;
                }
                switch (report.Punctuality)
                {
                    case "Low": puncImage.Src = "~/images/LowPerformance.png"; break;
                    case "Avarage": puncImage.Src = "~/images/AverageProgress.png"; break;
                    case "High": puncImage.Src = "~/images/HighProgress.png"; break;
                    default: puncImage.Alt = "Not Updated Yet"; break;
                }
                switch (report.Communication)
                {
                    case "Low": commImage.Src = "~/images/LowPerformance.png"; break;
                    case "Avarage": commImage.Src = "~/images/AverageProgress.png"; break;
                    case "High": commImage.Src = "~/images/HighProgress.png"; break;
                    default: commImage.Alt = "Not Updated Yet"; break;
                }
               
                PopulateSalaryInfo();
            }
            catch (Exception ex)
            {
                msgLabel.Text = "Employee not found by this code. Please try again";
                msgLabel.ForeColor = Color.Red;
            }

        }

        private void PopulateSalaryInfo()
        {
            //Employee salary Info
            Code code = new Code();
            code.CodeName = txtEmpCode.Text;
            SalaryManager salaryManager = new SalaryManager();
            Salary aSalary = new Salary();
            aSalary = salaryManager.GetEmployeeSalaryStructure(code);
            txtBasic.Value = aSalary.Basic.ToString();
            txtMedical.Value = aSalary.Medical.ToString();
            txtResidence.Value = aSalary.Residence.ToString();
            txtTransport.Value = aSalary.Transport.ToString();
            txtProfident.Value = aSalary.PF.ToString();
            txtMobile.Value = aSalary.Mobile.ToString();
            txtOthers.Value = aSalary.Others.ToString();
            txtGross.Value = aSalary.Gross.ToString();
        }
    }
}